package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.annotation.RedisCacheAnnotation;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.vo.NursingLevelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 护理等级Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
@Slf4j
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Resource
    StringRedisTemplate stringRedisTemplate;

    private final static String KEY = "Nursing:NursingLevel:all";

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {

        NursingLevel nursingLevel = nursingLevelMapper.selectNursingLevelById(id);
        return nursingLevel;
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelList(NursingLevelVo nursingLevel) {

        List<NursingLevelVo> nursingLevelVo = nursingLevelMapper.selectNursingLevelList(nursingLevel);

        return nursingLevelVo;


    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        // 当护理等级进行 新增的时候 也要删除缓存
        try {
            Boolean delete = stringRedisTemplate.delete(KEY);
            log.warn("缓存删除结果:{}",delete);
        }catch (Exception e){
            log.error("删除失败的结果:",e.getMessage());
        }
        return save(nursingLevel) ? 1 : 0;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        deleteCache(KEY);
        return updateById(nursingLevel) ? 1 : 0;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        deleteCache(KEY);
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        // 这里不添加缓存,因为根本就没有走这个接口
        return removeById(id) ? 1 : 0;
    }

    @RedisCacheAnnotation
    @Override
    public List<NursingLevel> listAll() {
        //加入redis缓存
     //   String s=null;
        List<NursingLevel> list =null;
//        try {
//            s = stringRedisTemplate.opsForValue().get(KEY);
//        } catch (Exception e){
//            log.error("缓存出现问题");
//        }
//        if (s!=null){
//            return JSONUtil.toList(s,NursingLevel.class);
//        }
        LambdaQueryWrapper<NursingLevel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingLevel::getStatus, 1);
        list = list(queryWrapper);
//        try {
//            stringRedisTemplate.opsForValue().set(KEY,JSONUtil.toJsonStr(list));
//        }catch (Exception e){
//            log.warn("分页查询的数据:{}",list);
//        }

        return list;
    }


    public Boolean deleteCache(String keyRedis){
        return stringRedisTemplate.delete(keyRedis);
    }
}
